package work.huangxin.share.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import work.huangxin.share.domain.sechand.SechandBuyGoods;
import work.huangxin.share.domain.sechand.SechandGoods;
import work.huangxin.share.mapper.SechandBuyGoodsMapper;
import work.huangxin.share.service.SechandBuyGoodsService;

import java.util.List;

/**
* @author 钟声浩
* @date 2023/3/25 21:57
*/
@Service
public class SechandBuyGoodsServiceImpl implements SechandBuyGoodsService {

    @Autowired
    private SechandBuyGoodsMapper sechandBuyGoodsMapper;

    //分页展示
    @Override
    public PageInfo<SechandBuyGoods> selectPageSechandBuyGoodsList
    (Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<SechandBuyGoods> sechandBuyGoodsList = sechandBuyGoodsMapper
                .selectPageSechandBuyGoodsList();
        return PageInfo.of(sechandBuyGoodsList);
    }

//    用户查询自己发布的求购商品
    @Override
    public List<SechandBuyGoods> selectSechandBuyGoodsByUserId(Integer userId) {
        return sechandBuyGoodsMapper.selectSechandBuyGoodsByUserId(userId);
    }

    //根据编号查询商品
    @Override
    public SechandBuyGoods selectSechandBuyGoodsById(Integer id) {
        return sechandBuyGoodsMapper.selectSechandBuyGoodsById(id);
    }

    //更新用户求购商品的信息
    @Override
    public int updateBuyGoods(SechandBuyGoods sechandBuyGoods) {
        return sechandBuyGoodsMapper.updateBuyGoods(sechandBuyGoods);
    }

    //根据关键字搜索求购商品
    @Override
    public PageInfo<SechandBuyGoods> getBuyGoodsByKeyWord(String keyword, Integer pageNum, Integer pageSize) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<SechandBuyGoods> list =  sechandBuyGoodsMapper.getBuyGoodsByKeyWord(keyword);
        return PageInfo.of(list);
    }

    //删除求购商品
    @Override
    public int deleteBuyGoods(Integer userId, Integer goodsId) {
        return sechandBuyGoodsMapper.deleteBuyGoods(userId, goodsId);
    }

    @Override
    public int deleteBuyGoodsByAdmin(Integer goodsId, Integer hasBuied) {
        return sechandBuyGoodsMapper.deleteBuyGoodsByAdmin(goodsId, hasBuied);
    }

    //下架求购商品
    @Override
    public int setBuyGoodsIsBuied(Integer userId, Integer goodsId) {
        return sechandBuyGoodsMapper.setBuyGoodsIsBuied(userId, goodsId);
    }

    @Override
    public int insertSechandBuyGoods(SechandBuyGoods sechandBuyGoods) {
        return sechandBuyGoodsMapper.insertSelective(sechandBuyGoods);
    }

    @Override
    public int updateSechandGoods(SechandBuyGoods sechandBuyGoods) {
        return sechandBuyGoodsMapper.updateBuyGoods(sechandBuyGoods);
    }

    @Override
    public int deletedBuyGoods(Integer goodsId) {
        return sechandBuyGoodsMapper.deleteBuyGoodsBy(goodsId);
    }
}
